/**
 * 235. 二叉搜索树的最近公共祖先
 */
public class No235 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        int min = Math.min(p.val, q.val);
        int max = Math.max(p.val, q.val);
        if (min > root.val) {
            return lowestCommonAncestor(root.right, p, q);
        } else if (max < root.val) {
            return lowestCommonAncestor(root.left, p, q);
        } else {
            return root;
        }
    }
}